package com.sctvcloud.beichuan.ui.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ruihang.generalibrary.utils.ListUtils;
import com.sctvcloud.beichuan.R;
import com.sctvcloud.beichuan.base.BaseActivity;
import com.sctvcloud.beichuan.beans.Emer;
import com.sctvcloud.beichuan.http.AbsListNetCallback;
import com.sctvcloud.beichuan.http.NetUtils;
import com.sctvcloud.beichuan.ui.activities.BroadcastActivity;
import com.sctvcloud.beichuan.ui.activities.EmergencyActivity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 首页紧紧发布
 */
public class EmergencyFragment extends ColumnBaseFragment  {

    @BindView(R.id.iv_emergency)
    protected TextView iv_emergency;
    @BindView(R.id.iv_earchquake)
    protected TextView iv_earchquake;
    @BindView(R.id.iv_weather)
    protected TextView iv_weather;

    public static final String EMER = "1930";
    public static final String EARTHQUAKE = "1920";
    public static final String WEATHER = "1921";


    List<Emer> emerList;

    public EmergencyFragment() {
        // Required empty public constructor
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_emergency, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void initViews(View v, @Nullable Bundle savedInstanceState) {
        channelUrl = HomeFragment.getInstance().getEmerUrl();
        getData();

    }

    @Override
    protected void onFragmentUnShowed(boolean isCallFromPause) {
        super.onFragmentUnShowed(isCallFromPause);

    }

    private void getData() {
        NetUtils.getNetAdapter().getEmergency(getOwnerName(), channelUrl, 0, new AbsListNetCallback<Emer>(null) {
            @Override
            public void onSuc(List<Emer> newsItems) {
                if (!ListUtils.isListValued(newsItems)) {
                    onError(null, null);
                    return;
                }
                emerList = newsItems;
            }

            @Override
            public void onError(Throwable ex, String errorMsg) {
            }

            @Override
            public void onEnd() {
            }
        });
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);

    }

    @OnClick(value = {R.id.iv_emergency, R.id.iv_earchquake, R.id.iv_weather})
    public void speakClick(View view) {
        switch (view.getId()) {
            case R.id.iv_emergency:
                for(Emer item: emerList){
                    if(item.getChannelId().equals(EMER)){
                        Intent intent = new Intent(getActivity(), BroadcastActivity.class);
                        intent.putExtra(BaseActivity.EX_URL, item.getChannelUrl());
                        intent.putExtra(BaseActivity.EX_TITLE, item.getChannelName());
                        startActivity(intent);
                    }
                }

                break;
            case R.id.iv_earchquake:
                for(Emer item: emerList){
                    if(item.getChannelId().equals(EARTHQUAKE)){
                        Intent intent = new Intent(getActivity(), EmergencyActivity.class);
                        intent.putExtra(BaseActivity.EX_URL, item.getChannelUrl());
                        intent.putExtra(BaseActivity.EX_TITLE, item.getChannelName());
                        startActivity(intent);
                    }
                }
                break;
            case R.id.iv_weather:
                for(Emer item: emerList){
                    if(item.getChannelId().equals(WEATHER)){
                        Intent intent = new Intent(getActivity(), EmergencyActivity.class);
                        intent.putExtra(BaseActivity.EX_URL, item.getChannelUrl());
                        intent.putExtra(BaseActivity.EX_TITLE, item.getChannelName());
                        startActivity(intent);
                    }
                }
                break;
        }
    }

    @Override
    protected boolean onFragmentShow(boolean isFirst, boolean isCallFromResume) {
        return super.onFragmentShow(isFirst, isCallFromResume);
    }

}
